package cn.stronglink.asset.module.alarm.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.message.websocket.MessageHandler;
import cn.stronglink.asset.model.Alarm;
import cn.stronglink.asset.model.AlarmType;
import cn.stronglink.asset.model.TRoom;
import cn.stronglink.asset.module.alarm.mapper.AlarmMapper;
import cn.stronglink.asset.module.alarm.vo.AlarmVo;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.basicdata.tRoom.mapper.TRoomMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class)
public class AlarmService {
	
	@Autowired
	private AlarmMapper alarmMapper;
	@Autowired
	private MessageHandler messageHandler;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private TRoomMapper tRoomMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AlarmType> getTypes() {
		return alarmMapper.getTypes();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<AlarmVo> getAlarmByParams(Page<AlarmVo> page, Map<String, Object> params) {
		 List<AlarmVo> list = alarmMapper.getAlarmByParams(page,params);
		 page.setRecords(list);
		 return page;
	}

	public void updageAlarmResult(Map<String, Object> params) {
		//取消3D中的报警
		AlarmVo vo=alarmMapper.qryById(Long.parseLong(params.get("id").toString()));
		if(vo!=null) {
			Map<String,Object> result = new HashMap<>();
			int alarmCount = alarmMapper.qryAlarmCountByRackId(vo.getRackId());
			if(alarmCount>1){
				result.put("alarmCount", 1);
			}else{
				result.put("alarmCount", 0);
			}
			List<String> clearServerId =new ArrayList<String>();
			clearServerId.add(vo.getAssetId());
			result.put("clearServerId", clearServerId);
			result.put("roomCode", vo.getRoomCode());
			result.put("rackCode", vo.getRackId()+"");
			String json = JSONObject.toJSONString(result);
			messageHandler.sendMessageToAllUsers(new TextMessage(json));
		}
	
		params.put("handleTime", new Date());
		params.put("handleBy", WebUtil.getCurrentUser());
		alarmMapper.updageAlarmResult(params);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public String getAlarmResultById(Long params) {	
		return alarmMapper.getAlarmResultById(params);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int qryAlarmCount() {
		return alarmMapper.qryAlarmCount();
	}

	public int insertAlarm(Alarm alarm) {		
		AlarmType type = alarmMapper.getAlarmType(alarm.getType());
		if(type!=null){
			alarm.setTypeId(type.getId());
//			Alarm a = alarmMapper.getAlarmByAssetId(alarm.getAssetId(),type.getId());
//			if(a!=null){
//				return 1;
//			}
		}else{
			AlarmType at= new AlarmType();
			at.setId(IdWorker.getId());
			at.setName(alarm.getType());
			alarmMapper.insertAlarmType(at);
			alarm.setTypeId(at.getId());
		}
		
		TRoom room = tRoomMapper.qryRoomByName(alarm.getRoom());
		Long rackId = itassetMapper.getRackIdByName(alarm.getRack());
		if(!"".equals(rackId)&&rackId!=null){
			alarm.setId(Long.parseLong(alarm.getAlarmId()));
			alarm.setRoomId(room.getId()+"");
			alarm.setRackId(rackId+"");
			alarm.setDesc(alarm.getInfo());
			alarm.setCreateTime(new Date());
			alarm.setStatus(1);		
			alarmMapper.insertAlarm(alarm);//插入报警
			System.out.println(JSON.toJSON(alarm)+"告警信息");
			Map<String,Object> result = new HashMap<>();	
			List<String> alarmServerIds = new ArrayList<String>();
			alarmServerIds.add(alarm.getAssetId());
			result.put("alarmServerIds", alarmServerIds);
			result.put("roomCode", room.getCode());
			result.put("rackCode", rackId+"");
			result.put("alarmCount", 0);
			String json = JSONObject.toJSONString(result);
			messageHandler.sendMessageToAllUsers(new TextMessage(json));
			return 2;
		}else{
			return 3;
		}
		
	}

	public void alarmCancel(Long id) {
		//取消3D中的报警
		AlarmVo vo=alarmMapper.qryById(id);
		if(vo!=null) {
			Map<String,Object> result = new HashMap<>();
			int alarmCount = alarmMapper.qryAlarmCountByRackId(vo.getRackId());
			if(alarmCount>1){
				result.put("alarmCount", alarmCount);
			}else{
				result.put("alarmCount", 0);
			}
			List<String> clearServerId =new ArrayList<String>();
			clearServerId.add(vo.getAssetId());
			result.put("clearServerId", clearServerId);
			result.put("roomCode",vo.getRoomCode());
			result.put("rackCode", vo.getRackId()+"");
			String json = JSONObject.toJSONString(result);
			messageHandler.sendMessageToAllUsers(new TextMessage(json));
			
			alarmMapper.alarmCancel(id);
		}
	   
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String, Object>> getAlarm(String roomCode) {
		return alarmMapper.getAlarm(roomCode);
	}

}
